﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Gman.Entity.Entities;
using Gman.WebApp.ServiceControllers;
using Gman.WebApp.Models;
using Gman.WebApp.Controllers.Base;
using Gman.WebApp.Models.Validator;

namespace Gman.WebApp.Controllers
{
    public class BajaActivoDetalleController : BaseController
    {

        private OperacionServiceController service = new OperacionServiceController();

        public BajaActivoDetalleModel GetBajaActivoDetalleModel(Guid id)
        {
            var cache = new CacheProvider();

            string key = id.ToString();
            var existingModel = cache.Get(key);
            if (existingModel == null)
            {
                existingModel = new BajaActivoDetalleModel(new BajaActivoDetalleBE());
                cache.Set(key, existingModel);
            }
            return (BajaActivoDetalleModel)existingModel;
        }

        public ActionResult UpdateField(string fieldName, string fieldValue, Guid id, string setterFormatOptions)
        {
            BajaActivoDetalleModel model = GetBajaActivoDetalleModel(id);
            SetterFormatOptions setterFormatOptionsEnum = SetterFormatOptionsHelper.Parse(setterFormatOptions);
            List<KeyValuePair<string, string>> validationList = new List<KeyValuePair<string, string>>();

            var setter = new PropertySetter<BajaActivoDetalleModel>.SetterOptions();
            setter.objectToSet = model;
            setter.property = fieldName;
            setter.val = fieldValue;
            setter.SetterFormatOptions = setterFormatOptionsEnum;
            setter.validationList = validationList;

            object valueWasSet = PropertySetter<BajaActivoDetalleModel>.SetFinalObject(setter);
            validationList.AddRange(new BajaActivoDetalleModelValidator().Validate(model));

            return Json
                (new
                {
                    result = true,
                    PropertyValidationList = validationList,
                }
                );
        }

        public ActionResult GetValidation(Guid id)
        {
            BajaActivoDetalleModel model = GetBajaActivoDetalleModel(id);
            List<KeyValuePair<string, string>> validationList = new List<KeyValuePair<string, string>>();

            validationList.AddRange(new BajaActivoDetalleModelValidator().Validate(model));

            return Json(new
            {
                PropertyValidationList = validationList,
            });
        }


        // GET: /BajaActivoDetalle/DoSearch           
        public ActionResult DoSearch(String cod, Guid activoGuid)
        {
            var cache = new CacheProvider();
            var activo = new BajaActivoDetalleController().GetBajaActivoDetalleModel(activoGuid);

            BienBE obj = new BienBE();
            obj.CodBien = cod;

            var bien = service.ObtenerBien(obj);
            if (bien != null)
            {
                activo.Bien = bien;
                activo.Action = "create";
                activo.ToolBar = new ToolBarModel();

                return PartialView("SearchActivo", activo);
            }

            return AjaxResultError(string.Format("No se encontró bien :{0} ", cod));
        }

    }
}
